/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */
package com.yunbiji.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yunbiji.interceptor.SessionInterceptor;
import com.yunbiji.model.Member_folders;
import com.yunbiji.model.Users;
import com.yunbiji.util.GetTime;

@Before(SessionInterceptor.class)
/**
 * 处理公司成员的增删改查类
 * @author niedaoxin
 *
 */
public class MemberFoldersController extends Controller
{
	public void index()
	{
		render("member.html");
	}
	/**
	 * 返回登陆界面
	 */
	public void login()
	{
		render("login.html");
	}
	/**
	 * 
	 * @param name
	 * @param pId
	 * @author niedaoxin
	 * @return String
	 * 在这里处理返回的list，将其转化成json数据
	 */
	public String common(String pId,String name)
	{
		String tel = getSessionAttr("username");
		List<Member_folders> list = Member_folders.dao.getMemberFolders(tel, pId);
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		int size = list.size() - 1;
		for (Member_folders members : list) {
			sb.append(" { ");
			sb.append(" id : '").append(String.valueOf(members.getInt("id"))).append("', ");
			sb.append(" name : '").append(members.getStr("h_name")).append("', ");
			sb.append(" isParent : true ");
			sb.append(" }");
			if(list.indexOf(members) < size){
				sb.append(", ");
			}
		}
		sb.append("]");
		return(sb.toString());
	}

	/**
	 * 获得trees中的参数，从表member_folders中查找数据经过处理后并且返回json数据
	 */
	public void getMembersFolders()
	{
		String pId;
		String pName;

		pId = getPara("ids");
		pName = getPara("n");
		String s=this.common(pId,pName);
		renderJson(s);

	}
	/**
	 * 接收新建文件的参数，并且将新建的文件夹保存的数据库中
	 * @param pIds 当前节点的id
	 * @param names 新增节点的名字
	 */
	public void saveMembersFolders()
	{
		String pId = getPara("pIds");
		String name= getPara("names");
		String tel = getSessionAttr("username");
		
		Member_folders member = new Member_folders();
		member.set("tel",tel);
		member.set("h_name", name);
		member.set("h_fafolder", Integer.valueOf(pId));
		member.set("h_foundtime",GetTime.dao.getTime());
		member.save();
		
		String id = String.valueOf(member.getInt("id"));
		renderText(id);
	}
	/**
	 * 删除选中的节点，如果此节点含有子节点则，不允许删除
	 */
	public void deleteMembersFolders()
	{
		String id=getPara("ids");
		if(id.equals("1"))
		{
			renderText("error");
		}
		else
		{
			if(Member_folders.dao.deleteMemberFolders(id))
			{
				renderText(id);
			}
			else
			{
				renderText("error");
			}
		}

	}
	/**
	 * 更新节点名称
	 */
	public void updateMembersFolders()
	{
		String id=getPara("ids");
		String name= getPara("names");
		String pId = getPara("pIds");
		Member_folders member = Member_folders.dao.findById(Integer.valueOf(id));
		if(null != name && !name.isEmpty())
		{
			//更新节点名称
			member.set("h_name", name).update();
			renderText(id);
		}
		else if(null != pId && !pId.isEmpty()){
			//更新上级模块
			member.set("h_fafolder", Integer.valueOf(pId)).update();
			renderText("success");
		}
		else
		{
			renderText("error");
		}
		
	}

	/**
	 * 从被点击的树节点里获取id和名字，根据当前的id去members表中检索出该文件夹下笔记的名字
	 */
	public void getMembers()
	{
		String pIdMem = getPara("ids");
		setSessionAttr("pIdMem",pIdMem);
		
		String tel = getSessionAttr("username");
		
		
		//根据pId，查找出对应的文件夹名称，因为第一个是固定的文件夹，不管是谁都拥有所以，需要区分一下
		String pNameMem;
		
		if(pIdMem.equals("1"))
		{
			pNameMem=Member_folders.dao.queryStrName(pIdMem);	
		}
		else
		{
			pNameMem=Member_folders.dao.queryStrName(pIdMem, tel);
		}
		
		setSessionAttr("pNameMem",pNameMem);
		
		
		// 确认详细结果分页
		Integer pagenoMem = getParaToInt(0);;
		
		if (null == pagenoMem || pagenoMem < 1)
		{
			pagenoMem = 1;
		}
		pIdMem=getSessionAttr("pIdMem");
		List<Users> list = Users.dao.getUsers(tel, pIdMem, pagenoMem);
		setSessionAttr("pagenoMem",pagenoMem);
		setAttr("pagenoMem", pagenoMem);
		setAttr("getMembers_Members", list);
		render("member.html");
	}
}
